// ==UserScript==
// @name           Ikariam Finances Tool
// @namespace      Ikariam-Finances-Tool
// @description    Open/close finance details
// @include        http://s*.ikariam.*/*view=finances
// @author			salomone
// @version			0.1.1
// ==/UserScript==
// History:
// v.0.1.0 - 20/03/2010
// * Some base functionality
// v.0.1.1 - 20/03/2010
// * Added picture to represent open/close



$ = document.getElementById;
var gameServer = top.location.host;
var url = self.location.href;
var divLabels = new Array('Scientists', 'Basic Costs', 'Supply costs', 'Total', 'Upkeep change', 'Gold' );
var buttonSrc = new Array('/skin/img/resource/btn_max.gif', '/skin/img/resource/btn_min.gif' );

function getGM_Value( variableName )	{
	return GM_getValue ( variableName );
}

function setGM_Value ( variableName, variableValue )	{
	GM_setValue ( variableName, variableValue );
}

function myGM_Log(strToLog)	{
	GM_log(strToLog);
}
function myGM_registerMenuCommand( menuText, callbackFunction )	{
	GM_registerMenuCommand(menuText, callbackFunction);
}

function addEvent(obj, evType, fn, bubble)	{
	if (obj.addEventListener)	{
		obj.addEventListener(evType, fn, bubble);
		return true;
	} 
	else if (obj.attachEvent)	{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	} 
	else {
		return false;
	}
}

function $x( xpath, root ) {
  var doc = root ? root.evaluate ? root : root.ownerDocument : document, next;
  var got = doc.evaluate( xpath, root||doc, null, 0, null ), result = [];
  switch (got.resultType) {
    case got.STRING_TYPE:
      return got.stringValue;
    case got.NUMBER_TYPE:
      return got.numberValue;
    case got.BOOLEAN_TYPE:
      return got.booleanValue;
    default:
      while (next = got.iterateNext())
        result.push( next );
      return result;
  }
}

function $X( xpath, root ) {
  var got = $x( xpath, root );
  return got instanceof Array ? got[0] : got;
}

function removeRecursively( parentElement, childElement )	{
	if ( childElement.firstChild )	{
		removeRecursively( childElement, childElement.firstChild );
	}	
	parentElement.removeChild( childElement );
}

function showHideObject(obj)	{
	if ( 'undefined' != obj && null != obj ) 	{
		if ( 'hidden' == obj.style.visibility )	{			
			obj.style.visibility = 'visible';
		}
		else	{
			obj.style.visibility = 'hidden';
		}
	}	
}

function openCloseObject(obj)	{
	if (obj.style.display == 'none') 	{
		obj.style.display = 'block';
		return;
	}	
	if (obj.style.display == 'block') 	{
		obj.style.display = 'none';
	}	
}

function showHideDataRows(obj)	{
	var myTableDataRows = $x("./../tr/td/..", obj );
	for ( j in myTableDataRows )	{
		//showHideObject(myTableDataRows[j]);		
		openCloseObject(myTableDataRows[j]);
	}
}

function createDiv( divId, divContent )	{
	var hiddenDiv = document.createElement('div');
	hiddenDiv.style.display = 'block';
	hiddenDiv.style.visibility = 'visible';
	hiddenDiv.id = 'undefined' == divId || null == divId ? 'hiddenDiv' + ( Math.random() * 100 ) : divId ;
	
	if (divContent)	{
		hiddenDiv.innerHTML = 'string' == typeof divContent ? divContent : divContent.toXMLString();
	}	
	
	return hiddenDiv;
}

function showHideTableDiv(obj)	{
	var labelDivId = obj.getAttribute('id');
	var tableDivId = labelDivId.replace(/labelDiv/, 'tableDiv');
	var tableDiv = document.getElementById( tableDivId );
	showHideObject( tableDiv );
}

function openCloseTableDiv(obj)	{
	var labelDivId = obj.getAttribute('id');
	var tableDivId = labelDivId.replace(/labelDiv/, 'tableDiv');
	var tableDiv = document.getElementById( tableDivId );
	openCloseObject( tableDiv );
	switchButton(labelDivId.replace(/labelDiv/, 'labelImage'));
}

function createImage(id, src)	{	
	var res = document.createElement('img');
	res.setAttribute('id', id);
	res.setAttribute('src', src);	
	return res;
}

function switchButton(imageId)	{
	var img = document.getElementById(imageId);
	var imgSrc = img.getAttribute('src');
	imgSrc = imgSrc == buttonSrc[0] ? buttonSrc[1] : buttonSrc[0];
	img.setAttribute('src', imgSrc);
}

if ( url.indexOf('view=finances') > 0 )	{
	main();
}

function main()	{	
	
	var mainDiv = $X(".//*[@id='mainview']");
	var myTableHeaders = $x("./table/tbody/tr/th/..", mainDiv);
	
	var myTableWithHeaderRow = null;		
	var i = 0;
	for ( i in myTableHeaders )	{
		myTableWithHeaderRow = $X("./../..", myTableHeaders[i]);	
		var labelDiv = createDiv( myTableWithHeaderRow.getAttribute('id')+i+'_labelDiv' , null);
		var tableDiv = createDiv( myTableWithHeaderRow.getAttribute('id')+i+'_tableDiv' , null);
		labelDiv.appendChild( document.createTextNode( divLabels[i] ) );
		labelDiv.appendChild( createImage(myTableWithHeaderRow.getAttribute('id')+i+'_labelImage', buttonSrc[1]) );
		tableDiv.appendChild( myTableWithHeaderRow );
		mainDiv.appendChild( labelDiv );
		mainDiv.appendChild( tableDiv );		
//		addEvent(labelDiv, 'click', function(obj){showHideTableDiv(this);}, true);
		addEvent(labelDiv, 'click', function(obj){openCloseTableDiv(this);}, true);
	}
	

/*	
	var i = j = 0;
	var myTableHeaderRows = $x(".//*[@id='mainview']/table/tbody/tr/th/..");	
	var myTableHeaderRow = null;
	var myTableDataRows = null;
	var myTableDataRow = null;
	var idStr = null;
	
	for ( i in myTableHeaderRows )	{
		myTableHeaderRow = myTableHeaderRows[i];
//		addEvent(myTableHeaderRow, 'click', function(obj){showHideDataRows(this);}, true);
		addEvent(myTableHeaderRow, 'click', function(obj){showHideDataRows(this);}, true);
		
		var myTableDataRows = $x("./../tr/td/..", myTableHeaderRow );
		for ( j in myTableDataRows )	{
			myTableDataRows[j].style.display = 'block';
		}		
	}	
*/	
	
	return;
}
